/**
 * 工具类函数
 * **/

// 判断是否为对象
function isObj(obj: any) {
  const str = typeof obj;
  return str === 'object' || (str === 'function' && obj !== null);
}

// 深拷贝函数
export function deepCopy(obj: any, wekmap = new WeakMap()) {
  if (typeof obj === 'symbol') return Symbol(obj.description);
  if (typeof obj === 'function') return obj;
  if (!isObj(obj)) return;
  if (wekmap.has(obj)) return wekmap.get(obj);
  const newObj: any = Array.isArray(obj) ? [] : {};
  wekmap.set(obj, newObj);
  for (const key in obj) {
    newObj[key] = deepCopy(obj[key], wekmap);
  }
  const symbolKeys = Object.getOwnPropertySymbols(obj);

  for (const val of symbolKeys) {
    newObj[val] = deepCopy(obj[val], wekmap);
  }
  return newObj;
}
